import { Component } from '@angular/core';
import { AlertController, IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { UtilsProvider } from "../../providers/utils/utils";
import { ValidateProvider } from "../../providers/validate/validate";
import { ConfigProvider } from "../../providers/config/config";
import { StorageProvider } from "../../providers/storage/storage";

/**
 * Generated class for the ReBuyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage({
  segment: 're-buy/:id',
  defaultHistory: ['PersonFinishtaskPage', 'HomePage']
})
@Component({
  selector: 'page-re-buy',
  templateUrl: 're-buy.html',
})

export class ReBuyPage {
  amoy_id: number;
  token: string;
  searchParams = {};
  userInfo = {};
  dataInfo = {};
  sentInfo = {};
  goodsNum='';
  constructor(public navCtrl: NavController,
    public httpService: HttpServicesProvider,
    public config: ConfigProvider,
    public validate: ValidateProvider,
    public alertCtrl: AlertController,
    public util: UtilsProvider,
    public storage: StorageProvider,
    public navParams: NavParams) {
  }
  //判断用户是否登录
  ionViewCanEnter(): boolean {
    return this.validate.checkLogin();
  }
  ionViewWillEnter() {
    

  }
  ionViewDidLoad() {
    this.amoy_id = this.navParams.get('id');
    this.token = this.config.getToken();
    this.getData();
    this.userInfo = JSON.parse(this.storage.get('userInfo'));
  }
  /**
 * 页面加载获取数据
 * */
  getData() {
    //获取任务信息列表
    let params = {
      token: this.token,
      employee_id: this.amoy_id
    };
    Object.assign(params, this.searchParams);

    let loading = this.util.loading();
    loading.show();
    this.httpService.requestPostData('/mul_mission/mission_detail', params, response => {
      loading.close()
      if (response['error'] == 200) {
        this.dataInfo = response['data']['mission_info'];
        //全部数据
      } else {
        this.util.presentAlert(response.message);
      }
    });
  }

  /**
    * 图片上传
    * */
    //跳转页面
    pageRedirect(page, data = {}) {
      this.navCtrl.push(page, data).then(success => {
          if (success !== true) {//页面需要登录后跳转
              this.navCtrl.push('LoginPage', data);
          }
      })
  }
  
  // 提交
  submitData(){
    let loading = this.util.loading();
    loading.show();
    this.httpService.requestPostData('/mul_mission/mission_re_buy',
    {
      token:this.token,
      mission_employee_id:this.amoy_id,
      goods:this.dataInfo['goods'],
      plan_num:this.goodsNum,
      plan_fee:this.dataInfo['goods_price']
    } , response => {
      loading.close()
      if (response['error'] == 200) {
        let loginAlert = this.alertCtrl.create({
          message: '复购成功，等待审核',
          buttons: [
              {
                  text: '知道了',
                  handler: () => {
                      this.navCtrl.push('ReBuyListPage')
                  }
              },
          ]
      });
      loginAlert.present();
      } else {
        this.util.presentAlert(response.message);
      }
    });
  }
}